package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;

import com.itheima.service.IAddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.itheima.entity.AddressBook;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private IAddressBookService addressBookService;

    @PostMapping
    public R addAddressBook(@RequestBody AddressBook addressBook, HttpSession session) {
        Long userId = (Long) session.getAttribute("UserId");
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return R.success("添加成功");
    }


    @PutMapping("/default")
    public R defaultAddress(@RequestBody AddressBook addressBook,HttpSession session) {
        Long id = addressBook.getId();
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper();
        qw.eq(AddressBook::getId, id);
        AddressBook one = addressBookService.getOne(qw);

        Object userId = session.getAttribute("UserId");
        LambdaQueryWrapper<AddressBook> qw1 = new LambdaQueryWrapper();
        qw1.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(qw1);
        for (AddressBook book : list) {
            book.setIsDefault(0);
            addressBookService.updateById(book);
        }
        one.setIsDefault(1);
        addressBookService.updateById(one);
        return R.success("设置成功");
    }

    @GetMapping("/{id}")
    public R selectedAddressid(@PathVariable Long id){
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper();
        qw.eq(AddressBook::getId, id);
        AddressBook one = addressBookService.getOne(qw);
//        if (one == null) {
//            return R.error("查询失败");
//        }
//
//        return R.success("设置成功");
//    }
    return one!=null?R.success(one):R.error("查询失败");
}
    @GetMapping("/default")
    public R selectedAddressid(HttpSession session){
        Long userId = (Long) session.getAttribute("UserId");
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper();
        qw.eq(AddressBook::getUserId, userId);
        qw.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(qw);
//        if (one == null) {
//            return R.error("查询失败");
//        }
//
//        return R.success("设置成功");
//    }
        return one!=null?R.success(one):R.error("查询失败");
    }

    @GetMapping("/list")
    public R selectedAddress(HttpSession session){
        Long userId = (Long) session.getAttribute("UserId");
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper();
        qw.eq(AddressBook::getUserId, userId);
        List<AddressBook> list = addressBookService.list(qw);
//        if (one == null) {
//            return R.error("查询失败");
//        }
//
//        return R.success("设置成功");
//    }
        return list!=null?R.success(list):R.error("查询失败");
    }


    @DeleteMapping
    public R deleteAddress(@RequestParam List<Long> ids) {

//                LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper();
//                qw.eq(AddressBook::getId, ids);
        boolean b = true;
        for (Long id : ids) {
            b = addressBookService.removeById(id);
        }
        if (b) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }



    }

    @PutMapping
    public  R updateAddress(@RequestBody AddressBook  addressBook){

        if (addressBook==null){
            return R.error("地址为空！！");
        }
//        Long id = addressBook.getId();
//        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
//        qw.eq(AddressBook::getId,id);
        return addressBookService.updateById(addressBook)?R.success("修改成功"):R.error("修改失败");
    }


}
